/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m002.bl;

import com.vaadin.terminal.gwt.server.WebApplicationContext;
import ep.persistence.controllers.T001UserJpaController;
import ep.persistence.controllers.T005PatientJpaController;
import ep.persistence.entities.T001User;
import ep.persistence.entities.T002Doctor;
import ep.persistence.entities.T005Patient;
import java.util.List;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author skyr
 */
@Path("bl002")
public class BL002Controller {

    @GET
    @Path("/patient")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_JSON})
    public T005Patient getPatientByAmka(@QueryParam("amka") String amka) {
        T005PatientJpaController jpaController = new T005PatientJpaController();
        Query query = jpaController.getEntityManager().createNamedQuery("T005Patient.findByAmka", T005Patient.class);
        query.setParameter("amka", amka);
        List result = query.getResultList();
        if (!result.isEmpty()) {
            return (T005Patient) query.getResultList().iterator().next();
        } else {
            return null;
        }
    }

    @GET
    @Path("/doctor")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces({MediaType.APPLICATION_JSON})
    public T002Doctor findDoctor(@QueryParam("username") String username) {
        T001UserJpaController jpaController = new T001UserJpaController();
        Query query = jpaController.getEntityManager().createNamedQuery("T001User.findByUsername", T001User.class);
        query.setParameter("username", username);
        T001User user = (T001User) query.getResultList().iterator().next();
        if (!user.getT002DoctorCollection().isEmpty()) {
            return user.getT002DoctorCollection().iterator().next();
        }
        return null;
    }
}
